

const {ccclass, property} = cc._decorator;

/**中间的时间指示器脚本 */
@ccclass
export default class TimePointer extends cc.Component {

    /**中间的时间指示器容器 */
    arrow: cc.Node = null;

    /**四个方向的指向图标容器 */
    pointer: cc.Node = null;

    /**倒计时的文字节点 */
    timeLabel: cc.Label = null;

    /**剩余多少秒后播放滴滴滴倒计时的声音 */
    alertTime = -1;


    /**倒计时时间 */
    time = -1;


    /**倒计时的滴滴的声音文件名 */
    didiFileName = 'timeup_alarm.mp3';


    onLoad() {
        const gameChild = this.node.getChildByName('game');
        this.arrow = gameChild.getChildByName('arrow');
        this.pointer = this.arrow.getChildByName('pointer');
        this.initPointer();
        
        this.timeLabel = this.arrow.getChildByName('lblTime').getComponent(cc.Label);
        this.timeLabel.string = '00';

        this.node.on(cc.vv.game_event_map.GAME_BEGIN, (data) => {
            this.initPointer();
        });

        this.node.on(cc.vv.game_event_map.GAME_CHUPAI, (data) => {
            this.initPointer();
            this.time = 1000000000;
            this.alertTime = 3;
        });
    }


    /**更新该谁出牌的中间的方位指向 */
    initPointer(){
        if(!cc.vv){
            return;
        }

        this.arrow.active = cc.vv.gameNetMgr.gamestate == 'playing';
        if(!this.arrow.active){
            return;
        }

        const turn = cc.vv.gameNetMgr.turn;
        const localIndex = cc.vv.gameNetMgr.getLocalIndex(turn);
        for(let i = 0; i < this.pointer.children.length; i++){
            let node = this.pointer.children[i];
            node.active = i == localIndex;
        }
    }

    update(dt){
        // 更新时间 播放催促音效
        if(this.time > 0){
            this.time -= dt;
            if(this.alertTime > 0 && this.time < this.alertTime){
                cc.vv.audioMgr.playSFX(this.didiFileName);
                this.alertTime = -1;
            }
            let pre = '';
            if(this.time < 0){
                this.time = 0;
                cc.vv.gameNetMgr.isAutomatic = true;
            }

            const t = Math.ceil(this.time);
            if(t < 10){
                pre = '0';
            }
            this.timeLabel.string = pre + t;
        }
    }
}
